package com.itboye.textileexpres.order;

import java.util.Observable;
import java.util.Observer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;

import com.itboye.textileexpes.beans.MyOrderBeans;
import com.itboye.textileexpes.presenter.MyOrderPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.alipaytest.ActivityPaymentMethod;
import com.itboye.textileexpres.base.BaseFragment;
import com.itboye.textileexpres.base.MainApplication;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.base.WebActivity;
import com.itboye.textileexpres.util.ClickListener;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.MyToast;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexpres.widget.XListView;
import com.itboye.textileexpres.widget.XListView.IXListViewListener;

/**
 * 
 * @author 全部
 * 
 */
public class FragmentOrderWhole extends BaseFragment implements Observer,
		IXListViewListener, ClickListener {
	int i;
	// TextView tv;
	XListView mylist;
	OrderWholeAdapter adapter;
	MyOrderPresenter myOrderPresenter;
	MyOrderBeans beans;
	OnClickListener listener;
	String type = "clean";
	int page_index = 1;
	int page_size = 10;
	private LinearLayout linearyoutIsnull;

	public FragmentOrderWhole(int i) {
		this.i = i;
	}

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_myorder;
	}

	@Override
	public void onMyActivityCreated() {
		// TODO Auto-generated method stub 订单搜索MyOrderShaerActivity
		// tv.setText("" + i);
		myOrderPresenter = new MyOrderPresenter(this);
		beans = new MyOrderBeans();
		beans.setUid(IsMobileNumPhone.isUid());
		beans.setQuery_status("0");
		beans.setPage_index(page_index + "");
		beans.setPage_size(page_size + "");
		myOrderPresenter.getInfo(beans);
		mylist.setPullLoadEnable(true);
		mylist.setPullRefreshEnable(true);
		mylist.setXListViewListener(this);
		IntentFilter newshead = new IntentFilter("updateorder");
		getActivity().registerReceiver(avartBroadcastReceiver, newshead);
		
		IntentFilter UpdateisOrder = new IntentFilter("orderUpdate");
		getActivity().registerReceiver(UpdateOrder, UpdateisOrder);
	}
	BroadcastReceiver avartBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			onStarOrder();

		};
	};
	
	BroadcastReceiver UpdateOrder = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			onStarOrder();

		};
	};
	@SuppressWarnings("unused")
	public void onStartTuk(int position) {
		Intent intent;
		String str = beans.getList().get(position).getQuery_status();
		intent = new Intent(getActivity(), ActvityOrderDetail.class);
		intent.putExtra("order_code", beans.getList().get(position)
				.getOrder_code());
		intent.putExtra("str", beans.getList().get(position).getQuery_status());
		startActivity(intent);
		

	}

	public  void  onStarOrder(){
		beans = new MyOrderBeans();
		beans.setUid(IsMobileNumPhone.isUid());
		beans.setQuery_status("0");
		beans.setPage_index(page_index + "");
		beans.setPage_size(page_size + "");
		myOrderPresenter.getInfo(beans);
	}
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == myOrderPresenter.Fenye_success) {

				beans = (MyOrderBeans) entity.getData();
				if (beans.getList().size() <= 0) {
					linearyoutIsnull.setVisibility(View.VISIBLE);
					mylist.setVisibility(View.GONE);
				} else if (beans.getList() == null) {
					linearyoutIsnull.setVisibility(View.VISIBLE);
					mylist.setVisibility(View.GONE);
				} else {
					adapter = new OrderWholeAdapter(getActivity(), beans, this);
					mylist.setAdapter(adapter);
					try {
						closeProgressDialog();
					} catch (Exception e) {
						// TODO: handle exception
					}
					onIsStop();
				}
			} else if (entity.getEventType() == myOrderPresenter.Fenye_fail) {
				onIsStop();
			}

			else if (entity.getEventType() == myOrderPresenter.Btn_success) {
				// beans=(MyOrderBeans) entity.getData();
				toast(entity.getData() + "");
				beans.setPage_index(page_index + "");
				beans.setPage_size(page_size + "");
				beans.setUid(IsMobileNumPhone.isUid());
				myOrderPresenter.getInfo(beans);
				adapter.notifyDataSetChanged();

			}else if(entity.getEventType()==MyOrderPresenter.Delete_success){
				beans.setUid(IsMobileNumPhone.isUid());
				beans.setQuery_status("0");
				page_size++;
				myOrderPresenter.getInfo(beans);
				adapter.notifyDataSetChanged();
				Intent intent2=new Intent("SendLog");
				getActivity().sendBroadcast(intent2);
				Intent intent3=new Intent("updateorder");
				getActivity().sendBroadcast(intent3);
				toast(""+entity.getData());
			}else if(entity.getEventType()==myOrderPresenter.Delete_fail){
				toast(""+entity.getData());
			}
		}
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		beans.setUid(IsMobileNumPhone.isUid());
		page_size = 1;
		beans.setQuery_status("0");
		myOrderPresenter.getInfo(beans);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		beans.setUid(IsMobileNumPhone.isUid());
		beans.setQuery_status("");
		page_size++;
		myOrderPresenter.getInfo(beans);
	}

	@Override
	public void onWuliu(View v) {
		// TODO Auto-generated method stub

		Intent intent = new Intent(MainApplication.context, WebActivity.class);
		intent.putExtra(
				"url",
				"http://api.guannan.itboye.com/public/web.php/Express/index?order_code="
						+ beans.getList().get((Integer) v.getTag())
								.getOrder_code() + "39"
						+ IsMobileNumPhone.isPasswrd());
		intent.putExtra("title", "物流查看");
		startActivity(intent);

		adapter.isPraised.put((Integer) v.getTag(), R.drawable.ic_launcher);
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onBtnSend(View v) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(getActivity(), ActivityPaymentMethod.class);
		intent.putExtra("price", beans.getList().get((Integer) v.getTag())
				.getPrice());
		intent.putExtra("orderNum", beans.getList().get((Integer) v.getTag())
				.getOrder_code());
		startActivity(intent);
	}

	@Override
	public void onCle(View v) {
		// TODO Auto-generated method stub

		myOrderPresenter.getDelete(IsMobileNumPhone.isUid(), beans.getList()
				.get((Integer) v.getTag()).getOrder_code());
		adapter.isPraised.put((Integer) v.getTag(), R.drawable.ic_launcher);
		beans.getList().remove((Integer) v.getTag());
		adapter.notifyDataSetChanged();
		
	}

	@Override
	public void onOkConfirm(View v) {
		// TODO Auto-generated method stub
		myOrderPresenter.getBtnSendOrder(IsMobileNumPhone.isUid(), beans
				.getList().get((Integer) v.getTag()).getOrder_code());
		adapter.notifyDataSetChanged();
	}

	public void onIsStop() {
		mylist.stopRefresh();
		mylist.stopLoadMore();
	}

}
